/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.utility.controller;

import com.tamnhat.lazyLoading.LazyAbstractDataModel;
import com.tamnhat.utility.entity.Billing;
import com.tamnhat.utility.entity.ServiceInfor;
import com.tamnhat.utility.model.SMSBatchesModel;
import com.tamnhat.utility.model.ServiceInforModel;
import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author Administrator
 */
@ViewScoped
public class ServiceInforController implements Serializable{
    private String phoneNumber;
    private String productName;
    private String createTime;
    private String cancelTime;
    private String status;
    private String partnerName;
    private DataModel listSMSBatches;
    private LazyDataModel<ServiceInfor> lazyDataModel;
    private ServiceInfor currentSI;    
    private String phoneNumberS;
    private DataModel listBilling;
    /**
     * Creates a new instance of ServiceInforController
     */
    public ServiceInforController() {
    }

    public LazyDataModel<ServiceInfor> getLazyDataModel() {
        return lazyDataModel;
    }

    public void setLazyDataModel(LazyDataModel<ServiceInfor> lazyDataModel) {
        this.lazyDataModel = lazyDataModel;
    }

    public DataModel getListBilling() {
        return listBilling;
    }

    public void setListBilling(DataModel listBilling) {
        this.listBilling = listBilling;
    }

    public DataModel getListSMSBatches() {
        return listSMSBatches;
    }

    public void setListSMSBatches(DataModel listSMSBatches) {
        this.listSMSBatches = listSMSBatches;
    }

    public ServiceInfor getCurrentSI() {
        return currentSI;
    }

    public void setCurrentSI(ServiceInfor currentSI) {
        this.currentSI = currentSI;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getCancelTime() {
        return cancelTime;
    }

    public void setCancelTime(String cancelTime) {
        this.cancelTime = cancelTime;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getPartnerName() {
        return partnerName;
    }

    public void setPartnerName(String partnerName) {
        this.partnerName = partnerName;
    }

    public String getPhoneNumberS() {
        return phoneNumberS;
    }

    public void setPhoneNumberS(String phoneNumberS) {
        this.phoneNumberS = phoneNumberS;
    }
    
    public String search(){
        try{
            List<ServiceInfor> listSI = ServiceInforModel.getAll(phoneNumberS.replaceAll("\\s+", ""));
            lazyDataModel = new LazyAbstractDataModel<ServiceInfor>(listSI);
            int i = listSI.size();
            if(i > 0){
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Có "+i+" bản ghi được tìm thấy!"));
            }
            else FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Không có bản ghi nào được tìm thấy!"));
            
        }
        catch(Exception e){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
            e.printStackTrace();
        }
        return "";
    }
    
    public String viewMsg(ServiceInfor s){
        currentSI = s;
        try{
            listSMSBatches = new ListDataModel(SMSBatchesModel.viewMsg(currentSI.getPhoneNumber(), currentSI.getProductId(),currentSI.getProductName()));
            int i = listSMSBatches.getRowCount();
            if(i > 0){
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Có "+i+" bản ghi được tìm thấy!"));
            }
            else FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Không có bản ghi nào được tìm thấy!"));
        }
        catch(Exception e){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
            e.printStackTrace();
        }
        return "";
    }
    
    public String viewBilling(ServiceInfor s){
        currentSI = s;
        try{
            listBilling = new ListDataModel(Billing.getAll(currentSI.getPhoneNumber(), currentSI.getProductName(),currentSI.getProductId()));
            int i = listBilling.getRowCount();
            if(i > 0){
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Có "+i+" bản ghi được tìm thấy!"));
            }
            else FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Không có bản ghi nào được tìm thấy!"));
        }catch(Exception e){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: "+e.getMessage()));
            e.printStackTrace();
        }
        return "";
    }
}
